package com.lxw.nonghe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxw.nonghe.annotation.RequiresPermission;
import com.lxw.nonghe.common.Result;
import com.lxw.nonghe.entity.MedicalInstitution;
import com.lxw.nonghe.service.MedicalInstitutionService;
import com.lxw.nonghe.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 医疗机构控制器
 */
@RestController
@RequestMapping("/medicalInstitution")
@RequiresPermission("institution:manage")
public class MedicalInstitutionController {

    @Autowired
    private MedicalInstitutionService medicalInstitutionService;

    @Autowired
    private LogUtils logUtils;

    @PostMapping("/create")
    public Result<Boolean> addMedicalInstitution(@RequestBody MedicalInstitution medicalInstitution) {
        try {
            // 检查机构编码是否已存在
            if (medicalInstitutionService.getMedicalInstitutionByInstitutionCode(medicalInstitution.getInstitutionCode()) != null) {
                logUtils.recordLog("医疗机构管理", "新增机构", "失败", "机构编码已存在:" + medicalInstitution.getInstitutionCode());
                return Result.error("该机构编码已存在，请重新填写");
            }

            boolean result = medicalInstitutionService.save(medicalInstitution);
            if (result) {
                logUtils.recordLog("医疗机构管理", "新增机构", "成功",
                        "机构ID:" + medicalInstitution.getInstitutionId() + ", 名称:" + medicalInstitution.getInstitutionName());
                return Result.success("医疗机构新增成功", true);
            } else {
                logUtils.recordLog("医疗机构管理", "新增机构", "失败", "保存数据库失败");
                return Result.error("医疗机构新增失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("医疗机构管理", "新增机构", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/delete/{id}")
    public Result<Boolean> deleteMedicalInstitution(@PathVariable Integer id) {
        try {
            MedicalInstitution institution = medicalInstitutionService.getById(id);
            boolean result = medicalInstitutionService.removeById(id);

            if (result) {
                logUtils.recordLog("医疗机构管理", "删除机构", "成功",
                        "机构ID:" + id + ", 名称:" + (institution != null ? institution.getInstitutionName() : "未知"));
                return Result.success("医疗机构删除成功", true);
            } else {
                logUtils.recordLog("医疗机构管理", "删除机构", "失败", "机构ID:" + id + "不存在");
                return Result.error("医疗机构删除失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("医疗机构管理", "删除机构", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/update")
    public Result<Boolean> updateMedicalInstitution(@RequestBody MedicalInstitution medicalInstitution) {
        try {
            // 检查机构编码是否已被其他机构使用
            MedicalInstitution existingInstitution = medicalInstitutionService.getMedicalInstitutionByInstitutionCode(medicalInstitution.getInstitutionCode());
            if (existingInstitution != null && !existingInstitution.getInstitutionId().equals(medicalInstitution.getInstitutionId())) {
                logUtils.recordLog("医疗机构管理", "更新机构", "失败", "机构编码冲突:" + medicalInstitution.getInstitutionCode());
                return Result.error("该机构编码已存在，请重新填写");
            }

            boolean result = medicalInstitutionService.updateById(medicalInstitution);
            if (result) {
                logUtils.recordLog("医疗机构管理", "更新机构", "成功",
                        "机构ID:" + medicalInstitution.getInstitutionId());
                return Result.success("医疗机构更新成功", true);
            } else {
                logUtils.recordLog("医疗机构管理", "更新机构", "失败",
                        "机构ID:" + medicalInstitution.getInstitutionId() + "不存在");
                return Result.error("医疗机构更新失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("医疗机构管理", "更新机构", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @GetMapping("/get/{id}")
    public Result<MedicalInstitution> getMedicalInstitutionById(@PathVariable Integer id) {
        try {
            MedicalInstitution medicalInstitution = medicalInstitutionService.getById(id);
            if (medicalInstitution != null) {
                /*logUtils.recordLog("医疗机构管理", "查询机构详情", "成功", "机构ID:" + id);*/
                return Result.success("医疗机构查询成功", medicalInstitution);
            } else {
                logUtils.recordLog("医疗机构管理", "查询机构详情", "失败", "机构ID:" + id + "不存在");
                return Result.error("未找到该医疗机构");
            }
        } catch (Exception e) {
            logUtils.recordLog("医疗机构管理", "查询机构详情", "失败", e.getMessage());
            return Result.error("查询异常: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result<Page<MedicalInstitution>> getMedicalInstitutionPage(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String institutionName) {
        try {
            Page<MedicalInstitution> page = medicalInstitutionService.getMedicalInstitutionPage(current, size, institutionName);
            /*logUtils.recordLog("医疗机构管理", "分页查询机构", "成功",
                    "页码:" + current + ", 大小:" + size + ", 机构名称:" + institutionName);*/
            return Result.success("医疗机构分页查询成功", page);
        } catch (Exception e) {
            logUtils.recordLog("医疗机构管理", "分页查询机构", "失败", e.getMessage());
            return Result.error("分页查询异常: " + e.getMessage());
        }
    }

    @PostMapping("/query")
    public Result<Page<MedicalInstitution>> queryMedicalInstitutions(
            @RequestBody MedicalInstitution queryParam,
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Page<MedicalInstitution> page = medicalInstitutionService.queryMedicalInstitutions(queryParam, current, size);
            /*logUtils.recordLog("医疗机构管理", "条件查询机构", "成功",
                    "条件:" + queryParam.toString() + ", 页码:" + current + ", 大小:" + size);*/
            return Result.success("医疗机构条件查询成功", page);
        } catch (Exception e) {
            logUtils.recordLog("医疗机构管理", "条件查询机构", "失败", e.getMessage());
            return Result.error("条件查询异常: " + e.getMessage());
        }
    }
}
